﻿namespace _04_C_中Foreach循环删除集合元素
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Console.Title = "Foreach的删除集合元素";

            List<string> colors = new List<string>() { "red", null, "green", "blue", "blue", "gray", "yellow", "white", "orange" };

            //foreach (string color in colors)
            //{
            //    if ("blue".Equals(color))
            //    {
            //        colors.Remove(color);
            //    }
            //}
            DeleteElement(colors);

            colors.ForEach(x => Console.WriteLine(x));

            Console.ReadKey();
        }

        static void DeleteElement(List<string> strs)
        {
            foreach (string str in strs)
            {
                if ("blue".Equals(str))
                {
                    strs.Remove(str);
                    DeleteElement(strs);
                    return;
                }
            }
        }
    }
}
